package com.yk.adverte.impl;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.yk.adverte.common.APP;
import com.yk.adverte.common.MsgBox;
import com.yk.adverte.model.bean.GpsBean;

import static com.yk.adverte.common.APP.isBackground;


/**
 * Created by Silence on 2017/4/5.
 */

public class GpsThread extends Thread{

    private Context mContext;
    private static Handler mHandler;
    private static AMapLocationClient locationClient = null;
    private static AMapLocationClientOption locationOption = new AMapLocationClientOption();

    /**
     * 初始化
     * @param context
     * @param handler
     */
    public GpsThread(Context context, Handler handler){
        this.mContext=context;
        this.mHandler=handler;
    }

    @Override
    public void run() {
        super.run();
        //初始化定位
        initLocation();
        startLocation();
    }

    /**
     * 发送消息
     */
    private void sendMessage(int what,Object object){
        Message msg= new Message();
        msg.what=what;
        msg.obj=object;
        mHandler.sendMessage(msg);
    }


    /**
     * 初始化定位
     *
     * @since 2.8.0
     * @author hongming.wang
     *
     */
    private void initLocation(){
        //初始化client
        locationClient = new AMapLocationClient(mContext);
        locationOption = getDefaultOption();
        //设置定位参数
        locationClient.setLocationOption(locationOption);
        // 设置定位监听
        locationClient.setLocationListener(locationListener);
    }

    /**
     * 默认的定位参数
     * @since 2.8.0
     * @author hongming.wang
     *
     */
    private AMapLocationClientOption getDefaultOption(){
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(2000);//可选，设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(false);//可选，设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mOption.setLocationCacheEnable(true); //可选，设置是否使用缓存定位，默认为true
        return mOption;
    }
    /**
     * 定位监听
     */
    AMapLocationListener locationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation location) {
            if (null != location) {
                //errCode等于0代表定位成功，其他的为定位失败，具体的可以参照官网定位错误码说明
                if (location.getErrorCode() == 0) {
                    //解析定位结果
                    GpsBean getGpsBean = new GpsBean();
                    getGpsBean.setAddress(location.getAddress());
                    getGpsBean.setCityName(location.getCity());
                    getGpsBean.setLatitude(location.getLatitude());
                    getGpsBean.setLongitude(location.getLongitude());
                    getGpsBean.setCityCode(location.getCityCode());
                    getGpsBean.setProvince(location.getProvince());
                    getGpsBean.setCountry(location.getCountry());
                    sendMessage(MsgBox.MSG_LOCATION_SUCCESS, getGpsBean);
                } else {
                    sendMessage(MsgBox.MSG_LOCATION_FAILED, location.getErrorInfo());
                }
            }
        }
    };

    /**
     * 开始定位
     *
     * @since 2.8.0
     * @author hongming.wang
     *
     */
    private void startLocation(){
        // 设置定位参数
        locationClient.setLocationOption(locationOption);
        // 启动定位
        locationClient.startLocation();
    }

    /**
     * 停止定位
     *
     * @since 2.8.0
     * @author hongming.wang
     *
     */
    public static void stopLocation(){
        // 停止定位
        locationClient.stopLocation();

    }

    /**
     * 销毁定位
     *
     * @since 2.8.0
     * @author hongming.wang
     *
     */
    public static void destroyLocation(){
        if (null != locationClient) {
            /**
             * 如果AMapLocationClient是在当前Activity实例化的，
             * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
             */
            locationClient.onDestroy();
            locationClient = null;
            locationOption = null;
        }
    }


}
